<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Sale_pack extends Model
{
    /*
     * 装箱单 sale_pack
     */
    public function getsalepack($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        if (isset($res['dname']) && $res['dname']) {
            //公司名订单号搜索
            $companyname = array_column(model('company')->getcompanyname($res), 'id');
            if ($companyname) {
                $maps['b.co_id'] = array('in', $companyname);
                $maps['b.co_branch_id'] = array('in', $companyname);
            }
            $maps['a.sale_id'] = array('like', '%' . removeBlank($res['dname']) . '%');//订单号
        } else {
            $maps = array();
        }
        $reswhere = $this->statuswhere($res['status']);
        $salewhere = $reswhere['salepackwhere'];
        if ($res['status'] == 1) {
            //配货中
            $join = [
                ['sale b', 'a.sale_id=b.id', 'LEFT'],//销售表
                ['sale_make c', 'c.sale_id=b.id', 'LEFT'],//配货表
            ];
            $saledata = Db::name('sale_detail')->alias('a')->join($join)->where(function ($query) use ($salewhere) {
                $query->where($salewhere);
            })->where(function ($query) use ($maps) {
                $query->whereOr($maps);
            })->field('a.sale_id,a.pro_id,a.prochar_id,a.qty,b.co_id,b.co_branch_id,b.priority,b.saler_id,b.create_id,c.pickup_at,c.process_at,c.create_at,c.checker_id')->page($page, $limit)->select();
            foreach ($saledata as $saledatakey => $saledataval) {
                $time=array($saledataval['create_at'],$saledataval['process_at'],$saledataval['pickup_at']);
                $timedate = gettime(max($time), time());//获取最大的数值
                $saledata[$saledatakey]['interval_time'] = $timedate['hour'] . ":" . $timedate['min'] . ':' . $timedate['sec'];
                //公司名称
                $saledata[$saledatakey]['company_name'] = getinfo($saledataval['co_id'], 'company')['name'];
                $saledata[$saledatakey]['company_branch_name'] = getValue('co_branch',['id'=>$saledataval['co_branch_id']],'name');
                //业务员
                $saledata[$saledatakey]['saler_name'] = getValue('admin',['id'=>$saledataval['saler_id']],'nickname');
                //对货员
                $checkername =  getValue('admin',['id'=>$saledataval['checker_id']],'nickname');
                $saledata[$saledatakey]['checker_name'] = $checkername ? $checkername : '-';
                //产品数据
                $saledata[$saledatakey]['pro_data'] = getProAndProchar($saledataval, $res['lang'], 48);
            }
            $count = Db::name('sale_detail')->alias('a')->join($join)->where(function ($query) use ($salewhere) {
                $query->where($salewhere);
            })->where(function ($query) use ($maps) {
                $query->whereOr($maps);
            })->count();
        } elseif ($res['status'] == 2 || $res['status'] == 3) {
            //是否显示自己的工作（显示自己的配货）
//            file_put_contents("test.txt","传入数据111 = ".json_encode($res)."\r\n", FILE_APPEND);
            if (isset($res['admin_id']) && $res['admin_id'] == 'true') {
                $salewhere['a.create_id'] = session('userid');
            }
            //已装箱
            $join = [
                ['sale b', 'a.sale_id=b.id', 'LEFT'],//销售表
            ];
            $saledata = Db::name('sale_make')->alias('a')->join($join)->where(function ($query) use ($salewhere) {
                $query->where($salewhere);
            })->where(function ($query) use ($maps) {
                $query->whereOr($maps);
            })->field('a.sale_id,a.checker_id,a.package_at,b.co_id,b.co_branch_id,b.priority,b.saler_id')->page($page, $limit)->select();

            foreach ($saledata as $saledatakey => $saledataval) {
                //公司名称
                $saledata[$saledatakey]['company_name'] = getinfo($saledataval['co_id'], 'company')['name'];
                //分公司名称
                $saledata[$saledatakey]['company_branch_name'] = getinfo($saledataval['co_branch_id'], 'co_branch')['name'];
                //一个销售装箱数据
                $datares = $this->getsalecount($saledataval['sale_id']);
                //产品数量
                $saledata[$saledatakey]['pronum'] = $datares['pronum'];
                //箱数
                $saledata[$saledatakey]['ctns'] = $datares['ctns'];
                //体积
                $saledata[$saledatakey]['volume'] = $datares['volume'];
                //重量
                $saledata[$saledatakey]['kgs'] = $datares['kgs'];
                //装箱员
                $saledata[$saledatakey]['create_name'] = $datares['create_name'];
                //装箱时间
                $saledata[$saledatakey]['create_date'] = getTimeDate($saledataval['package_at'], 12);
                //备注
                $saledata[$saledatakey]['remark'] = $datares['remark'];
                //业务员
                $saledata[$saledatakey]['saler_name'] = getinfo($saledataval['saler_id'], 'admin')['nickname'];
                $checkername = getinfo($saledataval['checker_id'], 'admin')['nickname'];
                $saledata[$saledatakey]['checker_name'] = $checkername ? $checkername : '-';
                //如果销售订单已经开发票了，就无法继续编辑装箱单
                $istrue = getValue('sale_invoice', ['sale_id' => $saledataval['sale_id']], 'number');
                if ($istrue) {
                    //无法编辑
                    $saledata[$saledatakey]['istrue'] = 1;
                } else {
                    //可以编辑
                    $saledata[$saledatakey]['istrue'] = 2;
                }
            }
            $count = Db::name('sale_make')->alias('a')->join($join)->where(function ($query) use ($salewhere) {
                $query->where($salewhere);
            })->where(function ($query) use ($maps) {
                $query->whereOr($maps);
            })->count();
        }
        //处理每一个标签的数字
        $arrcount = $this->arrcount($maps, $res['status']);
        return array('count' => $count, 'data' => $saledata, 'arrcount' => $arrcount);
    }

    //获取一个销售订单相关数据
    protected function getsalecount($sale_id)
    {
        //销售订单的装箱id
        $pack_ids = array_column(getables('sale_pack', ['sale_id' => $sale_id], 'id'), 'id');
        $mapids['id'] = $mapdetail['pack_id'] = ['in', $pack_ids];
        $pronum = count(Db::name('sale_pack_detail')->field('prochar_id')->where($mapdetail)->Distinct(true)->select());
        $ctns = Db::name('sale_pack')->where($mapids)->sum('ctns');
        $volume = Db::name('sale_pack')->where($mapids)->sum('volume');
        $kgs = Db::name('sale_pack')->where($mapids)->sum('kgs');
        $remark = getables('sale_pack_remark', $mapdetail, 'id,remark');
        $create_id = Db::name('sale_pack')->where(['sale_id' => $sale_id])->find()['create_id'];
        $create_name = getValue('admin', ['id' => $create_id], 'nickname');
        return array(
            'pronum' => $pronum, //产品数量
            'ctns' => $ctns,//箱数
            'volume' => $volume, //体积
            'kgs' => $kgs,  //重量
            'remark' => $remark, //备注
            'create_name' => $create_name,  //装箱员
        );
    }

    //设置条件
    protected function statuswhere($status)
    {
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $endToday = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $sevenday = mktime(0, 0, 0, date('m'), date('d') - 7, date('Y')) - 1;
        $mapsale = array();
        switch ($status) {
            //配货中
            case '1':
                //配货表
                $mapsale['c.package_at'] = 0;//表示还在配货中
                break;
            //已装箱(装箱录入完成1天)
            case '2':
                $mapsale['a.package_at'] = ['between', "$beginToday,$endToday"];//装箱必须是今天的
                $mapsale['a.delivery_at'] = 0;//表示还没有送货
                break;
            //已完成(装箱录入完成7天)
            case '3':
                $mapsale['a.package_at'] = ['between', "$sevenday,$endToday"];//装箱必须是7天内的
                break;
        }
        return array(
            'salepackwhere' => $mapsale,
        );
    }

    //获取头部的数字
    protected function arrcount($maps, $status)
    {
        $ones = $twos = $threes = array();
        switch ($status) {
            case '1':
                $ones = $maps;
                break;
            case '2':
                $twos = $maps;
                break;
            case '3':
                $threes = $maps;
                break;
        }
        $reswhereone = $this->statuswhere(1)['salepackwhere'];
        $reswheretwo = $this->statuswhere(2)['salepackwhere'];
        $reswherethree = $this->statuswhere(3)['salepackwhere'];
        $joinone = [
            ['sale b', 'a.sale_id=b.id', 'LEFT'],//销售表
            ['sale_make c', 'c.sale_id=b.id', 'LEFT'],//配货表
        ];
        $one = Db::name('sale_detail')->alias('a')->join($joinone)->where(function ($query) use ($reswhereone) {
            $query->where($reswhereone);
        })->where(function ($query) use ($ones) {
            $query->whereOr($ones);
        })->count();

        $jointwo = [
            ['sale b', 'a.sale_id=b.id', 'LEFT'],//销售表
        ];
        $two = Db::name('sale_make')->alias('a')->join($jointwo)->where(function ($query) use ($reswheretwo) {
            $query->where($reswheretwo);
        })->where(function ($query) use ($twos) {
            $query->whereOr($twos);
        })->count();

        $three = Db::name('sale_make')->alias('a')->join($jointwo)->where(function ($query) use ($reswherethree) {
            $query->where($reswherethree);
        })->where(function ($query) use ($threes) {
            $query->whereOr($threes);
        })->count();
        return array(
            'one' => $one == 0 ? '' : $one,
            'two' => $two == 0 ? '' : $two,
            'three' => $three == 0 ? '' : $three,
        );
    }

    //处理装箱单中编辑的数据的接口
    public function getsaleboxing($res)
    {
        $map['a.id'] = $res['sale_id'];
//        $map['b.checker_id'] = array('gt', 0);//含有指定的对货员
        //获取相关的数据
        $topdata = Db::name('sale')->alias('a')->join('nt_sale_make b', 'a.id=b.sale_id', 'left')->field('a.id,a.co_id,a.saler_id,b.checker_id,b.warehouse_id')->where($map)->find();
        $topdata['company_name'] = getValue('company',['id'=>$topdata['co_id']],'name');//公司名称
        $topdata['saler_name'] = getValue('admin',['id'=>$topdata['saler_id']],'nickname');//业务员
        $checkername = getValue('admin',['id'=>$topdata['checker_id']],'nickname');//对货员
        $topdata['checker_name'] = $checkername ? $checkername : '-';//对货员
        //产品id 角色id 产品的数量（获取销售单下面没有装箱的产品）
        $prodata = getables('sale_detail', ['sale_id' => $res['sale_id']], 'id,pro_id,prochar_id,qty');
        $ids = array_column(getables('sale_pack', ['sale_id' => $res['sale_id']], 'id'), 'id');//同一个销售id下的所有的装箱ids
        foreach ($prodata as $prodatakey => $prodataval) {
            //产品的详情数据
            $prosdatas = getProAndProchar($prodataval, $res['lang'], 48);
            //产品信息
            $prodata[$prodatakey]['img_url'] = $prosdatas['img_url'];
            $prodata[$prodatakey]['brand_name'] = $prosdatas['brand_name'];
            $prodata[$prodatakey]['model'] = $prosdatas['model'];
            $prodata[$prodatakey]['pro_name'] = $prosdatas['pro_name'];
            $prodata[$prodatakey]['prochar_qty'] = $prosdatas['prochar_qty'];//每箱装的数量
            $prodata[$prodatakey]['ctn_size'] = $prosdatas['ctn_size'];//箱规
            $prodata[$prodatakey]['ctn_kgs'] = $prosdatas['ctn_kgs'];//每箱重量
            $prodata[$prodatakey]['ctn_m3'] = $prosdatas['ctn_m3'];//每箱体积
            //查询每一个产品装了多少的数量（在同一个销售订单下的多个装箱单中）
            $maps['pro_id'] = $prodataval['pro_id'];
            $maps['prochar_id'] = $prodataval['prochar_id'];
            $maps['pack_id'] = ['in', $ids];
            $alreadyqty = Db::name('sale_pack_detail')->where($maps)->sum('qty');//同一个产品在一个销售单下的已装数量
            $prodata[$prodatakey]['quantityqty'] = $prodataval['qty'] - $alreadyqty;//总数量-已装数量=未装数量
        }
        //存放的位置必须是个人所在的仓库
        $warse_id=getValue('warehouse2admin',['admin_id'=>session('userid')],'warehouse_id');
        $topdata['warsedata'] = getables('warehouse_area', ['type_id' => 4,'warehouse_id'=>$warse_id], 'id,name');
        //获取这个销售订单下的装箱单
        $sale_packdata = $this->getprodata($res['sale_id'], $res['lang']);
        $area_id = '';
        if ($sale_packdata) {
            $area_id = $sale_packdata[0]['area_id'];
        }
        return array('topdata' => $topdata, 'prodata' => $prodata, 'sale_packdata' => $sale_packdata, 'area_id' => $area_id);
    }

    //装箱的入库操作
    public function addeditsaleboxing($res)
    {
        $ckdata = notEmpty(['area_id' => '待发区']);
        if ($ckdata['status'] == 2) {
            return $ckdata;
        }
        //如果已经开票了无法编辑
        $infosale_invoice = getValue('sale_invoice', ['sale_id' => $res['sale_id']], 'number');
        if ($infosale_invoice) {
            return array('msg' => '已经开了发票无法编辑装箱单', 'status' => 0);
        }

        foreach ($res['prodata'] as $prodatakey => $prodataval) {
            if ($prodataval['quantityqty'] != 0) {
                //表示这个产品还没装完装箱错误
                return array('status' => 2, 'msg' => '产品还未装完');
            }
        }
        $makeinfo = Db::name('sale_make')->field('id,warehouse_id')->where(['sale_id' => $res['sale_id']])->find();//配货信息
        $make_id = $makeinfo['id'];//配货id
        $warehouse_id = $makeinfo['warehouse_id'];//仓库id
        if ($res['names'] == 1) {
            //配货中的装箱 减少中转区和散货区的数据
            $code= $this->addstore($make_id, $warehouse_id, $res['prodata'], $res['area_id']);
            if($code['status']==0){
               return $code;
            }
        }
        //删除销售订单下的装箱单
        $ids = array_column(getables('sale_pack', ['sale_id' => $res['sale_id']], 'id'), 'id');
        $mappack['pack_id'] = $maps['id'] = ['in', $ids];
        $one = Db::name('sale_pack')->where($maps)->delete();
        Db::name('sale_pack_detail')->where($mappack)->delete();
        Db::name('sale_pack_remark')->where($mappack)->delete();
        if ($one !== false) {
            //销售订单的装箱数
            $ctn = $volumesum=0;
            foreach ($res['sale_packdata'] as $sale_packdatakey => $sale_packdataval) {
                if (!$sale_packdataval['id']) {
                    //添加
                    $insert['sale_id'] = $res['sale_id'];//销售id
                    $insert['make_id'] = $make_id;//配货id
                    $insert['number'] = $sale_packdataval['number'];//箱号
                    $insert['ctns'] = $sale_packdataval['ctns'];//装箱数量
                    $insert['volume'] = $sale_packdataval['volume'];//每箱体积
                    $insert['kgs'] = $sale_packdataval['ctn_kgs'] / $sale_packdataval['ctns'];//每箱重量=箱号重量/箱数
                    $insert['size'] = $sale_packdataval['ctn_size'];//尺寸
                    $insert['warehouse_id'] = $makeinfo['warehouse_id'];//仓库id
                    $insert['area_id'] = $res['area_id'];//待发区
                    $insert['create_id'] = $insert['update_id'] = session('userid');
                    $insert['create_at'] = $insert['update_at'] = time();
                    $sale_packid = Db::name('sale_pack')->strict(false)->insertGetId($insert);
                    $volumesum+=$sale_packdataval['ctns']*$sale_packdataval['volume'];//装箱体积
                } else {
                    //更新
                    $sale_packid = $updatepack['id'] = $sale_packdataval['id'];
                    $updatepack['sale_id'] = $res['sale_id'];//销售id
                    $updatepack['make_id'] = $make_id;//配货id
                    $updatepack['number'] = $sale_packdataval['number'];//箱号
                    $updatepack['ctns'] = $sale_packdataval['ctns'];//装箱数量
                    $updatepack['volume'] = $sale_packdataval['volume'];//每箱体积
                    $updatepack['kgs'] = $sale_packdataval['ctn_kgs'] / $sale_packdataval['ctns'];//每箱重量=箱号重量/箱数
                    $updatepack['size'] = $sale_packdataval['ctn_size'];//尺寸
                    $updatepack['area_id'] = $res['area_id'];//待发区
                    $updatepack['warehouse_id'] = $makeinfo['warehouse_id'];//仓库id
                    $updatepack['create_id'] = $updatepack['update_id'] = session('userid');
                    $updatepack['create_at'] = $updatepack['update_at'] = time();
                    Db::name('sale_pack')->strict(false)->insert($updatepack);
                    $volumesum+=$sale_packdataval['ctns']*$sale_packdataval['volume'];//装箱体积
                }
                $ctn += $sale_packdataval['ctns'];
                //装箱备注
                if ($sale_packdataval['remark']) {
                    $this->addinsertremark($sale_packid, $sale_packdataval);
                }
                //装箱明细表
                $this->addinsertdetail($sale_packid, $sale_packdataval);
            }
            $update['package_at'] = $update['maked_at'] = time();//装箱时间，配货完成
            Db::name('sale_make')->where('id', $make_id)->update($update);
            //更新这个销售订单装箱数
            Db::name('sale')->where('id', $res['sale_id'])->update(['ctn' => $ctn,'volume'=>$volumesum]);
        }
        return array('status' => 1, 'msg' => '装箱成功');
    }

    /*
     * /装箱时，对storearea处理相关数据
     * $make_id 配货id
     * $warehouse_id 仓库id
     * $data 装箱产品
     * $area_id 待发区id
     */
    protected function addstore($make_id, $warehouse_id, $data, $area_id)
    {
        $map['type_id']=array('in','1,2');
        $map['warehouse_id']=$warehouse_id;
        $warseids=implode(',',array_column(getables('warehouse_area',$map,'id'),'id'));
        //判断散货区和中转区数量是否足够
        foreach ($data as $datakey=>$dataval){
            $mapstore['warehouse_id']=$warehouse_id;
            $mapstore['area_id']= ['in',$warseids];
            $mapstore['pro_id']=$dataval['pro_id'];
            $mapstore['prochar_id']=$dataval['prochar_id'];
            $storedata=Db::name('store2area')->where($mapstore)->sum('qty');
            if($storedata<$dataval['qty']){
                return array('msg'=>'散货区或中转区数量不够，无法装箱','status'=>0);
            }
        }
        //减少中转区和散货区的数量体积,增加待发区数量体积
        foreach ($data as $datakey=>$dataval){
            $maparea['warehouse_id']=$areamap['warehouse_id'] = $warehouse_id;
            $areamap['area_id'] = ['in', $warseids];
            $maparea['pro_id']= $areamap['pro_id'] = $dataval['pro_id'];
            $maparea['prochar_id']=$areamap['prochar_id'] = $dataval['prochar_id'];
            $stareadata = Db::name('store2area')->where($areamap)->order('qty asc')->select();
            $ctn_m3 = $dataval['qty'] / $dataval['prochar_qty'] * $dataval['ctn_m3'];//箱数*体积
            $aa = $dataval['qty'];//需要减的数量
            foreach ($stareadata as $stareadatakey => $stareadataval) {
                $aa -= $stareadataval['qty'];//$aa-$stareadataval['qty'] 600 200 200 900
                $stareamap['warehouse_id'] = $stareadataval['warehouse_id'];
                $stareamap['area_id'] = $stareadataval['area_id'];
                $stareamap['pro_id'] = $stareadataval['pro_id'];
                $stareamap['prochar_id'] = $stareadataval['prochar_id'];
                if ($aa > 0) {
                    Db::name('store2area')->where($stareamap)->update(['qty'=>0]);
                    Db::name('store2area')->where($stareamap)->update(['volume'=>0]);
                } else if ($aa <= 0) {
                    $qty = $aa + $stareadataval['qty'];
                    Db::name('store2area')->where($stareamap)->setDec('qty', $qty);
                    Db::name('store2area')->where($stareamap)->setDec('volume', $ctn_m3);
                    break;
                }
            }
            //判断该产品是否存在待发区
            $maparea['area_id'] = $area_id;
            $stareainfo = Db::name('store2area')->where($maparea)->find();
            //增加待发区数量
            if ($stareainfo) {
                //增加数量体积
                Db::name('store2area')->where($maparea)->setInc('qty', $dataval['qty']);
                Db::name('store2area')->where($maparea)->setInc('volume', $ctn_m3);
            } else {
                //添加
                $insertrea['pro_id'] = $dataval['pro_id'];
                $insertrea['prochar_id'] = $dataval['prochar_id'];
                $insertrea['warehouse_id'] = $warehouse_id;
                $insertrea['area_id'] = $area_id;
                $insertrea['qty'] = $dataval['qty'];
                $insertrea['volume'] = $ctn_m3;
                Db::name('store2area')->insert($insertrea);
            }
        }
        return array('msg'=>'','status'=>1);
    }
    //装箱备注
    protected function addinsertremark($sale_packid, $sale_packdataval)
    {
        $insertremark['pack_id'] = $sale_packid;
        $insertremark['remark'] = $sale_packdataval['remark'];
        $insertremark['create_id'] = $insertremark['update_id'] = session('userid');
        $insertremark['create_at'] = $insertremark['update_at'] = session('userid');
        Db::name('sale_pack_remark')->insert($insertremark);
    }

    //装箱明细表
    protected function addinsertdetail($sale_packid, $sale_packdatas)
    {
        foreach ($sale_packdatas['data'] as $sale_packdataskey => $sale_packdatasval) {
            $insertdetail['pack_id'] = $sale_packid;
            $insertdetail['pro_id'] = $sale_packdatasval['pro_id'];
            $insertdetail['prochar_id'] = $sale_packdatasval['prochar_id'];
            $insertdetail['qty'] = $sale_packdatas['ctns'] * $sale_packdatasval['reality_qty'];//箱数*每箱实际装数量
            $insertdetail['create_id'] = $insertdetail['update_id'] = session('userid');
            $insertdetail['create_at'] = $insertdetail['update_at'] = session('userid');
            Db::name('sale_pack_detail')->insert($insertdetail);
        }
    }

    //装箱订单查看
    public function detailpack($res)
    {
        //销售订单数据
        $info = Db::name('sale')->where('id', $res['sale_id'])->field('id,co_id,co_branch_id,delivery_type_id')->find();
        $companyinfo = getinfo($info['co_id'], 'company');//公司名称
        $combranchinfo = getinfo($info['co_branch_id'], 'co_branch');//分公司名称
        $sale_invoiceinfo = Db::name('sale_invoice')->where('sale_id', $res['sale_id'])->find();//发票表
        $sale_makeinfo = Db::name('sale_make')->where('sale_id', $res['sale_id'])->find();//配货表
        //销售订单下装箱ids
        $sale_packids = array_column(Db::name('sale_pack')->where('sale_id', $res['sale_id'])->field('id')->select(), 'id');
        $maps['pack_id'] = ['in', $sale_packids];

        //获取销售订单下的相关数据
        $leftdata['sale_id'] = $res['sale_id'];//销售id
        $leftdata['companyname'] = $companyinfo['name'];//公司名称
        $leftdata['co_branch_name'] = $combranchinfo['name'];//分公司名称
        $leftdata['make_name'] = getinfo($sale_makeinfo['create_id'], 'admin')['nickname'];//配单主管
        $checkername = getinfo($sale_makeinfo['checker_id'], 'admin')['nickname'];//对货员
        $leftdata['checker_name'] = $checkername ? $checkername : '-';
        $infosalepack = Db::name('sale_pack')->where(['sale_id' => $res['sale_id']])->find();
        $leftdata['sale_packname'] = getValue('admin', ['id' => $infosalepack['create_id']], 'nickname');//装箱员
        $leftdata['area_name'] = getValue('warehouse_area', ['id' => $infosalepack['area_id']], 'name');//发货区
        $leftdata['remark'] = getables('sale_pack_remark', $maps, 'id,remark');//装箱备注
        //发票
        if ($sale_invoiceinfo['create_id']) {
            $leftdata['sale_invoicename'] = getValue('admin', ['id' => $sale_invoiceinfo['create_id']], 'nickname');
            $leftdata['sale_invoicedate'] = getTimeDate($sale_invoiceinfo['create_at'], 12);
        }
        //送货方式
        $delivery_typewhere['mkey'] = 'sale_delivery_type_' . $info['delivery_type_id'];
        $leftdata['delivergoods'] = getlangname('multi_lang', $delivery_typewhere, $res['lang']);//送货方式

        //销售订单下的产品数据
        $datapro = getables('sale_detail', ['sale_id' => $res['sale_id']], 'id,pro_id,prochar_id,qty');
        foreach ($datapro as $dataprokey => $dataproval) {
            //产品信息
            $prosdatas = getProAndProchar($dataproval, $res['lang'], 48);
            $datapro[$dataprokey]['img_url'] = $prosdatas['img_url'];
            $datapro[$dataprokey]['brand_name'] = $prosdatas['brand_name'];
            $datapro[$dataprokey]['model'] = $prosdatas['model'];
            $datapro[$dataprokey]['pro_name'] = $prosdatas['pro_name'];
            $datapro[$dataprokey]['prochar_qty'] = $prosdatas['prochar_qty'];
            $datapro[$dataprokey]['ctn_size'] = $prosdatas['ctn_size'];//尺寸
            $datapro[$dataprokey]['ctn_m3'] = $prosdatas['ctn_m3'];//体积
            $datapro[$dataprokey]['ctn_kgs'] = $prosdatas['ctn_kgs'];//重量
            //通过产品id和角色id获取取货位置
            $areaid = getValue('make_pickup_detail', ['pro_id' => $dataproval['pro_id'], 'prochar_id' => $dataproval['prochar_id']], 'area_id');
            $datapro[$dataprokey]['areaname'] = getValue('warehouse_area', ['id' => $areaid], 'name');
        }
        //获取这个销售订单下的装箱单
        $sale_packdata = $this->getprodata($res['sale_id'], $res['lang']);
        return array('leftdata' => $leftdata, 'sale_packdata' => $sale_packdata, 'prodata' => $datapro);
    }

    //一个销售订单下的装箱数据
    public function getprodata($sale_id, $lang)
    {
        $sale_packdata = getables('sale_pack', ['sale_id' => $sale_id], 'id,number,ctns,size as ctn_size,kgs,volume,area_id,update_id,update_at', 'number asc');
        foreach ($sale_packdata as $sale_packdatakey => $sale_packdataval) {
            //一个装箱单只会有一个备注
            $sale_packdata[$sale_packdatakey]['remark'] = getValue('sale_pack_remark', ['pack_id' => $sale_packdataval['id']], 'remark');
            //箱号的重量=每箱重量*箱数
            $sale_packdata[$sale_packdatakey]['ctn_kgs'] = $sale_packdataval['kgs'] * $sale_packdataval['ctns'];
            //箱号的体积=每箱体积*箱数
            $sale_packdata[$sale_packdatakey]['ctn_m3'] = $sale_packdataval['volume'] * $sale_packdataval['ctns'];
            $sale_packdata[$sale_packdatakey]['update_date'] = getTimeDate($sale_packdataval['update_at'],12);//编辑时间
            $sale_packdata[$sale_packdatakey]['updatename'] = getValue('admin',['id'=>$sale_packdataval['update_id']],'nickname');//编辑者
            //装箱单中详情
            $datapro = getables('sale_pack_detail', ['pack_id' => $sale_packdataval['id']], 'pro_id,prochar_id,qty');
            foreach ($datapro as $dataprokey => $dataproval) {
                if ($dataproval['pro_id'] > 0 && $dataproval['prochar_id'] > 0) {
                    //产品信息
                    $prosdatas = getProAndProchar($dataproval, $lang, 48);
                    $datapro[$dataprokey]['brand_id'] = $prosdatas['brand_id'];
                    $datapro[$dataprokey]['pro_name'] = $prosdatas['pro_name'];
                    $datapro[$dataprokey]['brand_name'] = $prosdatas['brand_name'];
                    $datapro[$dataprokey]['model'] = $prosdatas['model'];
                    $datapro[$dataprokey]['package_qty'] = $prosdatas['package_qty'];
                    $datapro[$dataprokey]['ctn_size'] = $prosdatas['ctn_size'];
                    $datapro[$dataprokey]['ctn_kgs'] = $prosdatas['ctn_kgs'];
                    $datapro[$dataprokey]['ctn_m3'] = $prosdatas['ctn_m3'];
                    $datapro[$dataprokey]['prochar_qty'] = $prosdatas['prochar_qty'];
                    $datapro[$dataprokey]['img_url'] = $prosdatas['img_url'];
                    $datapro[$dataprokey]['reality_qty'] = $dataproval['qty'] / $sale_packdataval['ctns'];//（数量/箱数）实际每箱数量
                    $datapro[$dataprokey]['check'] = true;
                } else {
                    //空箱数据
                    $datapro[$dataprokey]['pro_id'] = $datapro[$dataprokey]['prochar_id'] = '0';
                    $datapro[$dataprokey]['img_url'] = '/icon/0000/0000_48.jpg';
                    $datapro[$dataprokey]['reality_qty'] = $dataproval['qty'] / $sale_packdataval['ctns'];//实际每箱装的数量
                    $datapro[$dataprokey]['check'] = true;
                }
            }
            $sale_packdata[$sale_packdatakey]['data'] = $datapro;
        }
        return $sale_packdata;
    }

}